"use server";
import { revalidateTag } from "next/cache";

export async function serverFetch(api_type) {
  try {
    const response = await fetch(
      "http://localhost:3000/api/" + api_type + "/query?page=1&size=999",
      {
        next: {
          tags: [api_type],
        },
        method: "GET",
        headers: {
          "Content-Type": "application/json",
        },
      }
    );

    if (!response.ok) {
      // 这将激活最近的 `error.js` 错误边界
      throw new Error("获取数据失败");
    }

    const result = await response.json();
    console.log("成功:", result.data);
    return result;
  } catch (error) {
    console.error("错误:", error);
    throw error;
  }
}

export async function fetchDelete(api_type, id, keyword) {
  try {
    const response = await fetch(
      "http://localhost:3000/api/" + api_type + "/delete?" + keyword + "=" + id,
      {
        method: "DELETE",
        headers: {
          "Content-Type": "application/json",
        },
      }
    );

    if (!response.ok) {
      // 这将激活最近的 `error.js` 错误边界
      throw new Error("获取数据失败");
    }

    const result = await response.json();
    console.log("成功:", result.data);
    revalidateTag(api_type);
    return result;
  } catch (error) {
    console.error("错误:", error);
    throw error;
  }
}

export async function fetchUpdate(api_type, data) {
  try {
    const response = await fetch(
      "http://localhost:3000/api/" + api_type + "/update",
      {
        cache: "no-store",
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify(data),
      }
    );

    if (!response.ok) {
      // 这将激活最近的 `error.js` 错误边界
      throw new Error("获取数据失败");
    }

    const result = await response.json();
    console.log("成功:", result.data);
    revalidateTag(api_type);
    return result;
  } catch (error) {
    console.error("错误:", error);
    throw error;
  }
}

export async function OReceive(LPN) {
  try {
    const response = await fetch(
      "http://localhost:3000/api/Receive/outerDetailReceive?LPN=" + LPN,
      {
        cache: "no-store",
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
      }
    );

    if (!response.ok) {
      // 这将激活最近的 `error.js` 错误边界
      throw new Error("获取数据失败");
    }

    const result = await response.json();
    console.log("成功:", result.data);
    revalidateTag("outerDetail");
    return result;
  } catch (error) {
    console.error("错误:", error);
    throw error;
  }
}

export async function MDReceive(storehouseName, mdmID) {
  try {
    console.log(storehouseName);
    console.log(mdmID);
    const response = await fetch(
      "http://localhost:3000/api/Receive/materialDetailReceive?storehouseName=" +
        storehouseName +
        "&mdmID=" +
        mdmID,
      {
        cache: "no-store",
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
      }
    );

    if (!response.ok) {
      // 这将激活最近的 `error.js` 错误边界
      throw new Error("获取数据失败");
    }

    const result = await response.json();
    // console.log("成功:", result.data);
    revalidateTag("receiveMaterial");
    return result;
  } catch (error) {
    console.error("错误:", error);
    throw error;
  }
}
